package com.useeinfo.oa.modules.maintaining.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.maintaining.entity.FacilityMaintainSnap;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date: 2018/12/28 17:21
 * @Description:
 */
@Repository("facilityMaintainSnapDao")
public class FacilityMaintainSnapDao implements CrudDao<FacilityMaintainSnap> {

    private final static Logger logger = LoggerFactory.getLogger(FacilityMaintainSnapDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    public List<FacilityMaintainSnap> findByFacilityId(Long facilityId,String  serial) {
        List<FacilityMaintainSnap> facilityMaintainSnapList = this.entityManager.createQuery("select fm from FacilityMaintainSnap fm where fm.deleteStatus = false and fm.serial = :serial and fm.facilityId = :facilityId" , FacilityMaintainSnap.class)
                .setParameter("serial", serial)
                .setParameter("facilityId",facilityId).getResultList();
        if (CollectionUtils.isEmpty(facilityMaintainSnapList)) {
            return null;
        }
        return facilityMaintainSnapList;
    }



    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String maintainOrderId = queryHash.get("maintainOrderId");
        if (!StringUtils.isEmpty(maintainOrderId)) {
            conditionHash.put("(s.maintainOrder.id = ?{paramIndex}) ", StringConverters.null2Long(maintainOrderId));
        }
        String facilityId = queryHash.get("facilityId");
        if (!StringUtils.isEmpty(facilityId)) {
            conditionHash.put("(s.facilityId = ?{paramIndex}) ", StringConverters.null2Long(facilityId));
        }
        String serial = queryHash.get("serial");
        if (!StringUtils.isEmpty(serial)) {
            conditionHash.put("(s.serial = ?{paramIndex}) ", serial);
        }
        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from FacilityMaintainSnap s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<FacilityMaintainSnap> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select s from FacilityMaintainSnap s ", "order by s.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<FacilityMaintainSnap> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, FacilityMaintainSnap.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public FacilityMaintainSnap findModel(Long id) {
        return entityManager.find(FacilityMaintainSnap.class, id);
    }

    @Override
    public Integer add(FacilityMaintainSnap model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("FacilityMaintainSnapDaoImpl添加facilityMaintainSnap成功！");
        return 1;
    }

    @Override
    public Integer update(FacilityMaintainSnap model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        FacilityMaintainSnap facilityMaintainSnap = entityManager.find(FacilityMaintainSnap.class, id);
        entityManager.remove(facilityMaintainSnap);
        return 1;
    }

    @Override
    public Integer deleteLogic(FacilityMaintainSnap model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
